<?php
if (isset($_REQUEST["setbudget"]))
{
	$success = true;
	$validator = array("type" => "isPosted",
						"amount" => "isPostedInteger");

	foreach($_REQUEST["budget"] as $categoryId => $budgetInfo)
	{
		if (validateForm($validator, $budgetInfo))
		{
			if (!updateBudget($_SESSION["userid"], $categoryId, $budgetInfo["type"], $budgetInfo["amount"])) $success = false;
		}
	}
}

$categoryTree = getCategoryTree();
?>
			<div id="textcontainer">
				<h2>Kategóriák</h2>
				<p>
<?php
if ($categoryTree !== false)
{
	if (isset($success) && !$success) print "<div class=\"errortitle\">valami nem sikerult!</div>";
	print '<form name="setbudget" action="' . $_SERVER["SCRIPT_NAME"] . '?' . $_SERVER["QUERY_STRING"] . '" method="post">';
	print "<ul>";
	foreach ($categoryTree as $category)
	{
		print "<li>";
		print $category["category"]["name"] . "[" . $category["category"]["description"] ."]";

		print '<select name="budget[' . $category["category"]["id"] . '][type]">';
		foreach ($budgetTypes as $id => $name)
		{
			print '<option value="' . $id . '"' . ((isset($category["budget"]) && ($category["budget"]["type"] == $id))?' selected="selected"':'') . '>' . $name . '</option>';
		}
		print '</select>';
		print '<input type="text" name="budget[' . $category["category"]["id"] . '][amount]" value="' . ((isset($category["budget"]))?$category["budget"]["amount"]:'') . '"/>';

		if (isset($category["subcategoryList"]))
		{
			print "<ul>";
			foreach ($category["subcategoryList"] as $subcategory)
			{
				print "<li>" . $subcategory["category"]["name"] . "[" . $subcategory["category"]["description"] ."]";
				print '<select name="budget[' . $subcategory["category"]["id"] . '][type]">';
				foreach ($budgetTypes as $id => $name)
				{
					print '<option value="' . $id . '"' . ((isset($subcategory["budget"]) && ($subcategory["budget"]["type"] == $id))?' selected="selected"':'') . '>' . $name . '</option>';
				}
				print '</select>';
				print '<input type="text" name="budget[' . $subcategory["category"]["id"] . '][amount]" value="' . ((isset($subcategory["budget"]))?$subcategory["budget"]["amount"]:'') . '"/>';
			}
			print "</ul>";
		}
		print "</li>";
	}
	print "</ul>";
	print '<input type="submit" name="setbudget" value="Ment">';
	print "</form>";
}
else
{
	print "nincsenek kategóriák";
}
?>
				</p>
			</div>
			<!---->
